<!DOCTYPE html>
<html lang = "en">
<head>
    <meta charset = "UTF-8">
    <title>Title</title>

</head>


<body>
<!--=====================================================================-->
<!-- 练习,三个按钮变大变小和变色来改变div -->
<script>
    function divChange(effect) {
        let oDiv = document.getElementById('div-1');
        if (effect === 'bigger') {

            let height = parseInt(oDiv.style.getPropertyValue('height'));
            let width = parseInt(oDiv.style.getPropertyValue('width'));
            oDiv.style['height'] = height > 1000 ? height : height +10 +'px';   //用[xxx]来指定具体元素的名字
            oDiv.style['width'] = width > 1000 ? width : width + 10 +'px';

        }else if (effect === 'smaller') {
            let height = parseInt(oDiv.style.getPropertyValue('height'));
            let width = parseInt(oDiv.style.getPropertyValue('width'));
            oDiv.style['height'] = height <= 10 ? height :height - 10 + 'px';
            oDiv.style['width'] = width <= 10 ? width : width - 10 +'px';

        }else if (effect === 'color') {
            let arr = new Array(8);
            for (let i = 0; i < 8; i++) {
                arr[i] = getRndInteger(0,16);
            }
            oDiv.style['background'] = '#'+ getHex(arr);
        }
    }

    //随机获取[min,max]的数字
    function getRndInteger(min, max) {
        return Math.floor(Math.random() * (max - min + 1) ) + min;
    }

    //将列表转换为16进制字符串
    function getHex(arr) {
        let res = '';
        for (let i = 0; i < arr.length; i++) {
            let target = arr[i];
            if (target <10 ) {
                res += target;
            } else {
                switch(target) {
                    case 10:
                        res += 'a';
                        break;
                    case 11:
                        res += 'b';
                        break;
                    case 12:
                        res += 'c';
                        break;
                    case 13:
                        res += 'd';
                        break;
                    case 14:
                        res += 'e';
                        break;
                    case 15:
                        res += 'f';
                        break;
                }
            }
        }
        return res;
    }

</script>

<div>
    <input type="button" value="变大" onclick="changeDiv('bigger')">
    <input type="button" value="变小" onclick="changeDiv('smaller')">
    <input type="button" value="变色" onclick="changeDiv('color')">
    <div id="div-1" style="height: 100px; width: 100px; background: #000000;position: absolute; top: 60px;"></div>
</div>



</body>
</html>